package copy.code;

import com.sun.source.tree.Tree;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.TypeTag;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.TreeTranslator;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.ListBuffer;
import copy.application.Application;
import copy.express.pojo.Node;
import copy.express.pojo.NodeStack;
import copy.express.pojo.NodeType;
import copy.express.pojo.type.Type;
import copy.scan.ScanStrategy;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.TypeElement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 类作用: 处理类型转换相关
 */
public abstract class AbsTypetransformClassCode extends AbsClassCode implements GenerateMethodBodyCode {

    protected ScanStrategy scanStrategy =  (ScanStrategy)Application.getScanSingleBean().getStrategy(ScanStrategy.class);

    public AbsTypetransformClassCode(ProcessingEnvironment processingEnv) {
        super(processingEnv);
    }

    protected static Map<String,TypeTag> castTypeMap = new HashMap<>();
    static {
        castTypeMap.put("byte",TypeTag.BYTE);
        castTypeMap.put("short",TypeTag.SHORT);
        castTypeMap.put("char",TypeTag.CHAR);
        castTypeMap.put("int",TypeTag.INT);
        castTypeMap.put("long",TypeTag.LONG);
        castTypeMap.put("float",TypeTag.FLOAT);
        castTypeMap.put("double",TypeTag.DOUBLE);
    }

    /**
     * 作用: 在TypeTransform实现类中,添加一个成员属性.-->单例模式
     */
    public void genTransformSingleBean() {
        for (TypeElement e : ScanStrategy.typetransformMap.values() ) {
            //将接口的实现类的方法体,添加到缓存中
            //监视器访问
            JCTree tree = javacTrees.getTree(e);
            tree.accept(new TreeTranslator() {
                @Override
                public void visitClassDef(JCTree.JCClassDecl jcClassDecl) {
                    //
                    jcClassDecl.defs.stream()
                            //开始过滤: 只对类进行处理
                            .filter(it -> it.getKind().equals(Tree.Kind.METHOD))
                            //强制类型转换
                            .map(it -> (JCTree.JCMethodDecl) it)
                            //处理: 添加静态成员属性,即单例对象
                            .forEach(it -> {
                                        String methodName = it.getName().toString();
                                        if (methodName.equals("transform")) {
                                            String className = e.toString();
                                            className = className.substring(className.lastIndexOf(".") + 1);
                                            //new创建一个对象
                                            JCTree.JCNewClass jcNewClass = treeMaker.NewClass(
                                                    null,
                                                    List.nil(),
                                                    treeMaker.Ident(names.fromString(className)),
                                                    List.nil(),
                                                    null
                                            );
                                            //成员属性
                                            JCTree.JCVariableDecl singleVarible = treeMaker.VarDef(treeMaker.Modifiers(Flags.PUBLIC | Flags.STATIC), names.fromString("singleBean"), treeMaker.Ident(names.fromString(className)), jcNewClass);
                                            jcClassDecl.defs = jcClassDecl.defs.prepend(singleVarible);
                                        }
                                    }

                            );
                    super.visitClassDef(jcClassDecl);
                }
            });
        }

    }



}
